package com.itheima.controller;


import com.itheima.bean.vo.*;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin  //允许跨域
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 添加用户
     *
     * @param user 前端传递的请求参数对象：要添加的用户
     * @return
     */
    @PostMapping
   // @CrossOrigin
    public Result addUser(@RequestBody AddUser user) {

        //调用业务层：添加用户
        boolean bool = userService.addUserAndRole(user);

        //响应结果对象
        Result responseResult= new Result( Code.SAVE_ERR, "添加用户失败！");
        if (bool){
             responseResult = new Result(Code.SAVE_OK, true,"添加用户成功!");
        }
        return responseResult;
    }

    /**
     * 分页查询用户数据
     *
     * @param currentPage 页码
     * @param pageSize    每页条数
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    //@CrossOrigin  //允许跨域
    public Result selectUserByPage(@PathVariable Integer currentPage,
                                   @PathVariable Integer pageSize) {
        //创建分页对象
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(currentPage);
        pagination.setPageSize(pageSize);
        //分页查询用户
        PageBean pageBean = userService.queryUserByPage(pagination);
        //将要响应的结果封装到Result
        Result responseResult = new Result(Code.GET_ERR, "查询失败！");
        if (pageBean!=null){
             responseResult = new Result(Code.GET_OK, pageBean, "查询成功");
        }

        return responseResult;
    }

    //TODO：根据ID删除用户
    @DeleteMapping("/{id}")
    public Result deleteUser (@PathVariable Integer id){
        boolean result = userService.deleteUser(id);
        //响应结果对象
        Result responseResult= new Result( Code.DELETE_ERR, "删除用户失败！");
        if (result){
            responseResult = new Result(Code.DELETE_OK, true,"删除用户成功!");
        }
        return responseResult;
    }

    //TODO：修改用户
    @PutMapping
    public Result updateUser(@RequestBody AddUser addUser){
        boolean result =userService.updateUser(addUser);
        Result responseResult = new Result(Code.UPDATE_ERR, "修改用户失败！");
        if (result){
            responseResult = new Result(Code.UPDATE_OK,true,"修改用户成功！");
        }
        return responseResult;
    }

    //TODO：模糊查询
    @GetMapping("/{currentPage}/{pageSize}/{search}")
    //@CrossOrigin  //允许跨域
    public Result selectUserByPage(@PathVariable Integer currentPage,
                                   @PathVariable Integer pageSize,
                                   @PathVariable String search){
        //创建分页对象，封装数据
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(currentPage);
        pagination.setPageSize(pageSize);
        pagination.setSearch(search);
        //分页查询用户
        PageBean pageBean = userService.queryUserByPage(pagination);
        //将要响应的结果封装到Result
        Result responseResult = new Result(Code.GET_ERR, "查询失败！");
        if (pageBean!=null){
            responseResult = new Result(Code.GET_OK, pageBean, "查询成功");
        }

        return responseResult;
    }
}
